#include "../inc/Ui/SearchPlaceByPlaceForm.h"
#include "../inc/Ui/SearchPlaceForm.h"
#include "Ui/FormManager.h"
#include "../inc/Ui/WebForm.h"
#include"../inc/Utility/MyParserTiengViet.h"
using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

using namespace Osp::Graphics;
using namespace Osp::Media;

int SearchPlaceByPlaceForm::FormId = -1;

SearchPlaceByPlaceForm::SearchPlaceByPlaceForm(void) {
}

SearchPlaceByPlaceForm::~SearchPlaceByPlaceForm(void) {
}

bool SearchPlaceByPlaceForm::Initialize() {
	Form::Construct(L"SearchPlaceByPlaceForm");
	indexSelect=0;
	return true;
}

result SearchPlaceByPlaceForm::OnInitializing(void) {
	result r = E_SUCCESS;
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_SEARCH);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	// TODO: Add your initialization code here
	pEditFieldPlaceSearch = static_cast<EditField*> (GetControl(
			"IDC_EDITFIELD_PLACE"));

	pIconlistplace = static_cast<IconList *> (GetControl("IDC_ICONLISTPLACE"));
	pIconlistplace->SetItemTextColor(Color::COLOR_BLACK);
	if (pIconlistplace) {
		pIconlistplace->AddItemEventListener(*this);
	}
	taoVaHienThiIconList();
	return r;
}

void SearchPlaceByPlaceForm::taoVaHienThiIconList() {
	/*Khoi tao listFindPlace*/
	listKindPlace = new ArrayList();
	listKindPlace->Construct();
	listKindPlace->Add(*(new String(L"Art")));
		listKindPlace->Add(*(new String(L"Market")));

		listKindPlace->Add(*(new String(L"School")));
		listKindPlace->Add(*(new String(L"Jewelry")));
		listKindPlace->Add(*(new String(L"Stadium")));
		listKindPlace->Add(*(new String(L"Temple")));
		listKindPlace->Add(*(new String(L"Bank")));
		listKindPlace->Add(*(new String(L"Government")));
		listKindPlace->Add(*(new String(L"Company")));
		listKindPlace->Add(*(new String(L"Library")));

		listKindPlace->Add(*(new String(L"Park")));
		listKindPlace->Add(*(new String(L"Coffee")));
		listKindPlace->Add(*(new String(L"University")));

		listKindPlace->Add(*(new String(L"Gas Station")));
		listKindPlace->Add(*(new String(L"Store")));
		listKindPlace->Add(*(new String(L"Pharmacy")));
		listKindPlace->Add(*(new String(L"Hospital")));
		listKindPlace->Add(*(new String(L"ATM")));
		listKindPlace->Add(*(new String(L"Golf Club")));
		listKindPlace->Add(*(new String(L"Restaurant")));
		listKindPlace->Add(*(new String(L"Airport")));
		listKindPlace->Add(*(new String(L"Car repair")));
		listKindPlace->Add(*(new String(L"Theater")));
		listKindPlace->Add(*(new String(L"Zoo")));
		listKindPlace->Add(*(new String(L"Insurance")));

	/*Bitmaps*/
	Bitmap *bitMapList[listKindPlace->GetCount()];
	for (int i = 0; i < listKindPlace->GetCount(); i++) {
		String bitmapName;
		String *namePlace=static_cast<String*>(listKindPlace->GetAt(i));
		bitmapName.Append(namePlace->GetPointer());
		bitmapName.Append(L".jpg");
		bitMapList[i]=GetBitmapN(bitmapName);
	}
	/*Icon LIst*/
	for(int j=0;j<listKindPlace->GetCount();j++)
	{
		String* namePlace=static_cast<String*>(listKindPlace->GetAt(j));
		String itemText=namePlace->GetPointer();
		pIconlistplace->AddItem(&itemText,bitMapList[j],bitMapList[j],j);
	}

	/*Delete bitmap*/
	for(int h=0;h<listKindPlace->GetCount();h++)
	{

		delete bitMapList[h];
	}
	Draw();
	Show();
}
Bitmap*
SearchPlaceByPlaceForm::GetBitmapN(const Osp::Base::String& name) {
	Bitmap* pBitmap = null;
	Image* pImage = new Image();

	String fullname(L"/Res/ImageKindPlace/");
	fullname.Append(name);
	//AppLog("Get bitmap:%S", fullname.GetPointer());
			pImage->Construct();

			if(fullname.EndsWith(L"jpg"))
			{
				pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"bmp"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"png"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_ARGB8888);
	}
	else if (fullname.EndsWith(L"gif"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	delete pImage;

	return pBitmap;

}

result SearchPlaceByPlaceForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here
	if (listKindPlace != null){
		listKindPlace->RemoveAll(true);
		delete listKindPlace;
	}
	return r;
}

void SearchPlaceByPlaceForm::OnActionPerformed(const Osp::Ui::Control &source,
		int actionId) {
	// TODO: Add your implementation codes here
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);

	switch (actionId) {
	case ID_SOFTKEY_SEARCH:
		SearchPlace();
		break;
	case ID_SOFTKEY_BACK:
		/*pFrame->SetCurrentForm(*pFormParent);
		 pFormParent->RequestRedraw(true);
		 */
		pFormManager->SetCurrentForm(SearchPlaceForm::GetFormId());
		break;
	}
}

void SearchPlaceByPlaceForm::OnItemStateChanged(const Osp::Ui::Control &source,
		int index, int itemId, Osp::Ui::ItemStatus status) {
	if (itemId > -1) {
		indexSelect = itemId;
	}

}
void SearchPlaceByPlaceForm::SearchPlace() {
	String temp;
	temp.Append(L"Item ");
	String* name=static_cast<String *>(listKindPlace->GetAt(indexSelect));
	temp.Append(name->GetPointer());
	temp.Append(indexSelect);
	temp.Append(L" Selected");
	AppLog("%d", indexSelect);

	FormManager *pFormManager = null;
	Form *pWebForm = null;
	FormManager::GetInstance(pFormManager);

	pFormManager->SetCurrentForm(WebForm::GetFormId());
	result r = pFormManager->GetForm(WebForm::GetFormId(),pWebForm);
	if (IsFailed(r)) {
		return;
	}
	String address;
	MyParserTiengViet *pTiengViet=new MyParserTiengViet();
	address= pTiengViet->TranformSignToUnsignInTiengViet(pEditFieldPlaceSearch->GetText());
	static_cast<WebForm*> (pWebForm)->showResultSearchPlaceByPlceInMap(address,name->GetPointer());

		}
